
using GoodDotNet.Data.Models;
using GoodDotNet.Core.Entity;
using GoodDotNet.Core.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Reflection;


namespace GoodDotNet.Core.Data
{
    public class MyDbContext : DbContext
    {
       
        private ConnectionString _connectionString;
        public MyDbContext(ConnectionString conns)
        {
            _connectionString = conns;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            if (!optionsBuilder.IsConfigured)
            {

                switch (_connectionString.Type)
                {
                    case "SqlServer":
                        //optionsBuilder.UseSqlServer(_connectionString);

                        break;
                    case "Oracle":
                        //optionsBuilder.UseOracle(_connectionString, b => b.UseOracleSQLCompatibility("11"));
                        break;
                    case "MySql":
                        optionsBuilder
                              //.UseLazyLoadingProxies()
                              //.UseMySQL(_connectionString);
                              .UseMySql(_connectionString.Conn, new MySqlServerVersion(new Version(10, 2, 39)));
                        break;
                    default:
                        break;
                }


            }
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.UseCollation("utf8_general_ci");
            // 获取程序集中所有满足约定的实体类
            var entityTypes = Assembly.GetExecutingAssembly().GetTypes()
               .Where(t => typeof(IEntity).IsAssignableFrom(t) && t != typeof(IEntity));

            // 遍历实体类，注册到 DbContext 中
            foreach (var entityType in entityTypes)
            {
                modelBuilder.Entity(entityType);
            }
        

        }
        protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.DefaultTypeMapping(typeof(TableColumn));
            //.Properties<Currency>()
            //.HaveConversion<CurrencyConverter>();
        }
        public void Detach()
        {
            ChangeTracker.Entries().ToList().ForEach(aEntry =>
            {
                if (aEntry.State != EntityState.Detached)
                    aEntry.State = EntityState.Detached;
            });
        }
        public override int SaveChanges()
        {
            int count = base.SaveChanges();
            Detach();
            return count;
        }
        public async override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            int count = await base.SaveChangesAsync(cancellationToken);
            Detach();
            return count;
        }
        public virtual DbSet<UserRole> UserRole { get; set; }
         public virtual DbSet<Permission> Permission { get; set; }
        public virtual DbSet<Role> Role { get; set; }
        public virtual DbSet<RoleModulePermission> RoleModulePermission { get; set; }
        public virtual DbSet<AdminRoleModule> AdminRoleModule { get; set; }
        public virtual DbSet<AdminModule> AdminModule { get; set; }

        public virtual DbSet<AdminRole> AdminRole { get; set; }
        public virtual DbSet<AdminUser> AdminUser { get; set; }
    
		  public virtual DbSet<AdminUserRole> AdminUserRole { get; set; }
		


		  public virtual DbSet<Test> Test { get; set; }
		


		  public virtual DbSet<User> User { get; set; }
		

}

}
